package main

import (
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"fmt"
)

func HelloServer(wri http.ResponseWriter, req *http.Request) {
	req.ParseForm()
   	if len(req.Form) == 0 {
		bytes, _ := ioutil.ReadFile("./client.htm")
		io.WriteString(wri, string(bytes))
	} else if req.Form["r"][0] == "1" {
		content := req.Form["data"][0]
		result := 0

		for _, ch := range content {
			result += int(ch)
		}

		fmt.Fprintf(wri, "%d", result * len(content))
	}
}

func main() {
	http.HandleFunc("/test/", HelloServer)
	
	fmt.Println("Listening...")
	err := http.ListenAndServe(":12345", nil)

	if err != nil {
		log.Fatal("ListenAndServer: ", err.Error())
	}
}

/*需要在后台一直运行
go run server.go &
http://localhost:12345/test/*  *代表任意内容
*/
